namespace Gamov.DeleteSdData.Main.Services.Impl
{
    using System.Collections.Generic;
    using System.IO;
    using Gamov.DeleteSdData.Main.Frame.Attributes;
    using Gamov.DeleteSdData.Main.Services;
    using Microsoft.Extensions.Logging;

    [Component(typeof(IDiskService), Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton)]
    public class DiskService : IDiskService
    {

        private readonly ILogger<DiskService> _logger;

        public DiskService(ILogger<DiskService> logger)
        {
            _logger = logger;
        }

        /// <summary>
        /// 获取外置磁盘的标识
        /// </summary>
        /// <returns>new string[] { @"I:\新建文件夹\" }</returns>
        public IEnumerable<string> GetRemovableDeviceID()
        {
            try
            {
                ICollection<string> removableDiskNames = new List<string>();

                var allDirves = DriveInfo.GetDrives();
                //检索计算机上的所有逻辑驱动器名称
                foreach (var item in allDirves)
                {
                    if (item.IsReady && item.DriveType == DriveType.Removable)
                    {
                        removableDiskNames.Add(item.Name);
                    }
                }
                //removableDiskNames = new string[] { @"E:\Test\MyCameraTest" };
                _logger.LogInformation($"DeviceIDs = [{string.Join(",", removableDiskNames)}]");
                return removableDiskNames;
            }
            catch (System.Exception ex)
            {
                _logger.LogError(ex, "An error occurred while getting removable device IDs.");
                return new List<string>();
            }
        }
    }
}
